﻿using JadeFramework.Core.Domain.CodeBuilder.MySQL;
using JadeFramework.Core.Extensions;
using NodaTime;
using System;
using System.Collections.Generic;
using System.Text;

namespace MsSystem.Web.Areas.Sys.Infrastructure
{
    public static class CodeStringBuild
    {
        public static DateTime ToCstTime(this DateTime time)
        {
            Instant now = SystemClock.Instance.GetCurrentInstant();
            var shanghaiZone = DateTimeZoneProviders.Tzdb["Asia/Shanghai"];
            return now.InZone(shanghaiZone).ToDateTimeUnspecified();
        }
        private static StringBuilder Init(string username = null)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("//------------------------------------------------------------------------------");
            sb.AppendLine("// <auto-generated>");
            sb.AppendLine("//     此代码由工具生成。");
            sb.AppendLine($"//     创建人：{username}");
            sb.AppendLine($"//     创建时间：{DateTime.Now.ToCstTime()}");
            sb.AppendLine("//     说明：");
            sb.AppendLine("// </auto-generated>");
            sb.AppendLine("//------------------------------------------------------------------------------");
            return sb;
        }

        /// <summary>
        /// 获取类型
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        private static string GetType(TableColumn column)
        {
            string type = "";
            switch (column.DATA_TYPE)
            {
                case "tinyint":
                    type = "byte";
                    break;
                case "int":
                    type = "int";
                    break;
                case "bigint":
                    type = "long";
                    break;
                case "varchar":
                case "text":
                    type = "string";
                    break;
                case "datetime":
                    type = "DateTime";
                    break;
                case "decimal":
                    type = "double";
                    break;
                case "char":
                    type = "Guid";//char规定为GUID类型
                    break;
                default:
                    throw new Exception("存在不支持的类型");
            }
            if (column.IS_NULLABLE == "YES")
            {
                if (type != "string")
                {
                    type += "?";
                }
            }
            return type;
        }

        /// <summary>
        /// 创建MYSQL实体 FOR GM NH
        /// </summary>
        /// <param name="search"></param>
        /// <param name="table"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static StringBuilder GetModel(TableSearch search, Table table, List<TableColumn> list)
        {
            StringBuilder sb = Init(search.CreateUser);
            sb.AppendLine("using JadeFramework.Core.Dapper;");
            sb.AppendLine("using System;");
            sb.AppendLine("using System.ComponentModel.DataAnnotations;");
            sb.AppendLine("using System.ComponentModel.DataAnnotations.Schema;");
            sb.AppendLine();
            sb.AppendLine($"namespace {search.Namespace}.Model");
            sb.AppendLine("{");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine($"    /// {table.TABLE_COMMENT}");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    [Table(\"" + table.TABLE_NAME + "\")]");
            sb.AppendLine($"    public class {table.TABLE_NAME.ToHump()}");
            sb.AppendLine("    {");
            foreach (var item in list)
            {

                sb.AppendLine("        /// <summary>");
                sb.AppendLine($"        /// {item.COLUMN_COMMENT}");
                sb.AppendLine("        /// </summary>");
                if (item.COLUMN_KEY == "PRI")
                {
                    if (item.DATA_TYPE == "bigint" || item.DATA_TYPE == "int")
                    {
                        sb.AppendLine("        [Key, Identity]");
                    }
                    else
                    {
                        sb.AppendLine("        [Key]");
                    }
                }
                sb.AppendLine($"        public {GetType(item)} {item.COLUMN_NAME.ToHump()} {{ get; set; }}");
                sb.AppendLine();
            }
            sb.AppendLine("    }");

            sb.AppendLine($"    public class {table.TABLE_NAME.ToHump()}Mapper : ClassMapper<{table.TABLE_NAME.ToHump()}>");
            sb.AppendLine("    {");
            sb.AppendLine($"        public {table.TABLE_NAME.ToHump()}Mapper()");
            sb.AppendLine("        {");
            sb.AppendLine($"            Table(\"" + table.TABLE_NAME + "\");");
            sb.AppendLine("            AutoMap();");
            sb.AppendLine("        }");
            sb.AppendLine("    }");

            sb.AppendLine("}");
            sb.AppendLine();
            return sb;
        }

        public static StringBuilder GetRepository(TableSearch search, Table table)
        {
            StringBuilder sb = Init(search.CreateUser);
            sb.AppendLine("using JadeFramework.Dapper;");
            sb.AppendLine("using JadeFramework.Dapper.SqlGenerator;");
            sb.AppendLine($"using {search.Namespace}.IRepository;");
            sb.AppendLine($"using {search.Namespace}.Model;");
            sb.AppendLine("using System.Data;");
            sb.AppendLine();
            sb.AppendLine($"namespace {search.Namespace}.Repository");
            sb.AppendLine("{");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine($"    /// {table.TABLE_COMMENT} 仓储");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine($"    public class {table.TABLE_NAME.ToHump()}Repository : DapperRepository<{table.TABLE_NAME.ToHump()}>, I{table.TABLE_NAME.ToHump()}Repository");
            sb.AppendLine("    {");

            sb.AppendLine($"        public {table.TABLE_NAME.ToHump()}Repository(IDbConnection connection, SqlGeneratorConfig config) : base(connection, config)");
            sb.AppendLine("        {");
            sb.AppendLine("        }");

            sb.AppendLine("    }");
            sb.AppendLine("}");
            sb.AppendLine();
            return sb;
        }

        public static StringBuilder GetIRepository(TableSearch search, Table table)
        {
            StringBuilder sb = Init(search.CreateUser);
            sb.AppendLine("using JadeFramework.Dapper;");
            sb.AppendLine($"using {search.Namespace}.Model;");
            sb.AppendLine();
            sb.AppendLine($"namespace {search.Namespace}.IRepository");
            sb.AppendLine("{");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine($"    /// {table.TABLE_COMMENT} 仓储接口");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine($"    public interface I{table.TABLE_NAME.ToHump()}Repository : IDapperRepository<{table.TABLE_NAME.ToHump()}>");
            sb.AppendLine("    {");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            sb.AppendLine();
            return sb;
        }

    }
}
